package com.weetop.akly.activity.meessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.akly.R;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.ApproveInfoBean;
import com.weetop.akly.bean.ApproveItem;
import com.weetop.akly.presenter.ShenpiDetailPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.GlideUtil;
import com.weetop.akly.utils.widget.AlertDialogIOS;
import com.weetop.akly.view.ShenpiDetailView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShenpiDetailActivity extends BaseActivity<ShenpiDetailPresent> implements ShenpiDetailView {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_data_title)
    TextView tvDataTitle;
    @BindView(R.id.rcy_info)
    RecyclerView rcyInfo;
    @BindView(R.id.tv_bdbh)
    TextView tvBdbh;
    @BindView(R.id.tv_sqry)
    TextView tvSqry;
    @BindView(R.id.tv_ssbm)
    TextView tvSsbm;
    @BindView(R.id.tv_yczt_title)
    TextView tvYcztTitle;
    @BindView(R.id.tv_yczt)
    TextView tvYczt;
    @BindView(R.id.rl_view01)
    RelativeLayout rlView01;
    @BindView(R.id.tv_bkrq_title)
    TextView tvBkrqTitle;
    @BindView(R.id.tv_bkrq)
    TextView tvBkrq;
    @BindView(R.id.rl_view02)
    RelativeLayout rlView02;
    @BindView(R.id.tv_bksj_title)
    TextView tvBksjTitle;
    @BindView(R.id.tv_bksj)
    TextView tvBksj;
    @BindView(R.id.rl_view03)
    RelativeLayout rlView03;
    @BindView(R.id.tv_reason)
    TextView tvReason;
    @BindView(R.id.rv_fj)
    RecyclerView rvFj;
    @BindView(R.id.btn_chexiao)
    TextView btnChexiao;
    @BindView(R.id.rl_chexiao)
    FrameLayout rlChexiao;
    @BindView(R.id.tv_sqry_title)
    TextView tvSqryTitle;
    @BindView(R.id.tv_sfhgs_title)
    TextView tvSfhgsTitle;
    @BindView(R.id.tv_sfhgs)
    TextView tvSfhgs;
    @BindView(R.id.rl_view04)
    RelativeLayout rlView04;
    @BindView(R.id.tv_fj_nodata)
    TextView tvFjNodata;
    @BindView(R.id.ll_fj)
    LinearLayout llFj;
    private String approveId;
    private ApproveItem item;
    private String[] fujians;
    private AlertDialogIOS myDialog;
    private String title;
    private String modelId;
    private String type;

    @Override
    protected ShenpiDetailPresent createPresenter() {
        return new ShenpiDetailPresent(this, this);
    }

    private CommonRecyclerAdapter<ApproveInfoBean.ProcessBean> spAdapter;
    private CommonRecyclerAdapter<String> fjAdapter;
    private ArrayList<ApproveInfoBean.ProcessBean> sprlist = new ArrayList<>();
    private List<String> fjList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shenpi_detail);
        ButterKnife.bind(this);
        setTitleInfo("审批详情");
        Intent intent = getIntent();
        approveId = intent.getStringExtra("approve_id");
        type = intent.getStringExtra("type");
        item = (ApproveItem) intent.getSerializableExtra("approve_info");
        modelId = item.getModel_id();
//        if (!TextUtils.isEmpty(approveId)) {
//            mPresenter.getShenpiDetail(approveId + "");
//        }

        rcyInfo.setLayoutManager(new LinearLayoutManager(mActivity));
        spAdapter = new CommonRecyclerAdapter<ApproveInfoBean.ProcessBean>(mActivity, R.layout.item_liucheng, sprlist) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, ApproveInfoBean.ProcessBean item, int position) {
                // 审批状态：-1拒绝0未审核1审核通过2转交
                String state = item.getStatus() == -1 ? "已拒绝"
                        : item.getStatus() == 0 ? "暂未审核"
                        : item.getStatus() == 1 ? "审核通过"
                        : item.getStatus() == 2 ? "已转交"
                        : "";
                String opinion = TextUtils.isEmpty(item.getOpinion()) ? "" : "（" + item.getOpinion() + "）";
                String title = item.getLeader_name() + "  " + state + opinion;
                helper.setText(R.id.tv_sp_title, title);
                String timeFormat = item.getCreate_time();
                helper.setText(R.id.tv_sp_time, timeFormat);
            }
        };
        rcyInfo.setAdapter(spAdapter);

        //初始化附件
        rvFj.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
        fjAdapter = new CommonRecyclerAdapter<String>(mActivity, R.layout.item_fj_img, fjList) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, String item, int position) {
                ImageView imageView = helper.getView(R.id.iv_image);
                GlideUtil.load(mActivity, imageView, item);
            }
        };
        rvFj.setAdapter(fjAdapter);

        initAletDialog();
        initData();
    }

    private void initData() {
//        List<ApproveInfoBean.FormBean> form = o.getData().getForm();
//        ApproveInfoBean.InfoBean info = o.getData().getInfo();
        title = item.getTitle();
//        List<ApproveInfoBean.ProcessBean> process = o.getData().getProcess();
        // 状态：-3删除-2撤销-1拒绝0未审核1审核中999审核通过
        int spStatus = item.getStatus();
        if (spStatus == 0&& !type.equals("1")) {
            rlChexiao.setVisibility(View.VISIBLE);
        }

        String model_id = item.getModel_id();//  model_id  1:补卡申请	2:请假申请	3:外出申请	4:外勤申请
        tvBdbh.setText(item.getCode());
        tvSqry.setText(item.getApprove_user_name());
        tvSsbm.setText(item.getApprove_user_department());
        if (StringUtils.equals("1", model_id)) {
            String status = item.getStatus() == 1 ? "正常"
                    : item.getStatus() == 3 ? "迟到"
                    : item.getStatus() == 4 ? "早退"
                    : item.getStatus() == 5 ? "旷工"
                    : item.getStatus() == 0 ? "缺省"
                    : "缺省";
            tvYczt.setText(status);
            tvBkrq.setText(item.getMake_day());
            tvBksj.setText(item.getUpdate_time());
        } else {
            tvYczt.setText(item.getStart_time());
            tvBkrq.setText(item.getEnd_time());
            if (StringUtils.equals("5", model_id)) {
                tvBksj.setText(item.getCount_hour() + "小时");
            } else {
                tvBksj.setText(item.getCount_day() + "天");
            }
        }
        if (StringUtils.equals("3", model_id)) {
            String goComp = StringUtils.equals("1", item.getIs_return()) ? "回"
                    : StringUtils.equals("0", item.getIs_return()) ? "不回"
                    : "不回";
            tvSfhgs.setText(goComp);
        }
        tvReason.setText(item.getLeave_reason());
        if (!TextUtils.isEmpty(item.getEnclosure_url())) {
            fujians = item.getEnclosure_url().split(",");
            fjList = Arrays.asList(fujians);
        } else {
            fjList = new ArrayList<>();
        }

        switch (model_id) {
            case "1":
                tvSqryTitle.setText("补卡人员：");
                tvYcztTitle.setText("异常状态：");
                tvBkrqTitle.setText("补卡日期：");
                tvBksjTitle.setText("补卡时间：");
                break;
            case "2":
                tvSqryTitle.setText("请假人员：");
                tvYcztTitle.setText("开始时间：");
                tvBkrqTitle.setText("结束时间：");
                tvBksjTitle.setText("请假天数：");
                break;
            case "3":
                tvSqryTitle.setText("外出人员：");
                tvYcztTitle.setText("开始时间：");
                tvBkrqTitle.setText("结束时间：");
                tvBksjTitle.setText("合计时长：");
                rlView04.setVisibility(View.VISIBLE);
                tvSfhgsTitle.setText("是否回公司：");
                llFj.setVisibility(View.GONE);
                break;
            case "4":
                tvSqryTitle.setText("外勤人员：");
                tvYcztTitle.setText("开始时间：");
                tvBkrqTitle.setText("结束时间：");
                tvBksjTitle.setText("合计时长：");
                llFj.setVisibility(View.GONE);
                break;
            case "5":
                tvSqryTitle.setText("加班人员：");
                tvYcztTitle.setText("开始时间：");
                tvBkrqTitle.setText("结束时间：");
                tvBksjTitle.setText("加班时长：");
                break;
            default:
                tvSqryTitle.setText("申请人员：");
                rlView01.setVisibility(View.GONE);
                rlView02.setVisibility(View.GONE);
                rlView03.setVisibility(View.GONE);
                rlView04.setVisibility(View.GONE);
                break;
        }
        ApproveInfoBean.ProcessBean process = new ApproveInfoBean.ProcessBean();
        process.setLeader_id(Integer.parseInt(item.getApprove_user_id()));
        process.setLeader_name(item.getApprove_user_name());
        process.setStatus(item.getStatus());
        process.setCreate_time(item.getCreate_time());
        spAdapter.add(process);
//        spAdapter.addAll(process);
        if (fjList.size() > 0) {
            fjAdapter.replaceAll(fjList);
        } else {
            rvFj.setVisibility(View.GONE);
            tvFjNodata.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void getShenpiDetailSuccess(BaseModel<ApproveInfoBean> o) {
    }


    private void initAletDialog() {
        myDialog = new AlertDialogIOS(this).builder();
    }


    @OnClick(R.id.btn_chexiao)
    public void onViewClicked() {
        if (BaseUtils.fastClick()) {
            myDialog.setGone().setTitle("提示").setMsg("是否确定此审核？").setNegativeButton("拒绝", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StringUtils.equals("1", modelId)) {
                        mPresenter.shMakeCard(item.getLeave_id() + "", "-1");
                    } else if (StringUtils.equals("2", modelId)) {
                        mPresenter.shApprove(item.getLeave_id() + "", "-1");
                    } else if (StringUtils.equals("3", modelId)) {
                        mPresenter.shGoOut(item.getLeave_id() + "", "-1");
                    } else if (StringUtils.equals("4", modelId)) {
                        mPresenter.shWQ(item.getLeave_id() + "", "-1");
                    } else if (StringUtils.equals("5", modelId)) {
                        mPresenter.shOverTime(item.getLeave_id() + "", "-1");
                    }
                }
            }).setPositiveButton("确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (StringUtils.equals("1", modelId)) {
                        mPresenter.shMakeCard(item.getLeave_id() + "", "1");
                    } else if (StringUtils.equals("2", modelId)) {
                        mPresenter.shApprove(item.getLeave_id() + "", "1");
                    } else if (StringUtils.equals("3", modelId)) {
                        mPresenter.shGoOut(item.getLeave_id() + "", "1");
                    } else if (StringUtils.equals("4", modelId)) {
                        mPresenter.shWQ(item.getLeave_id() + "", "1");
                    } else if (StringUtils.equals("5", modelId)) {
                        mPresenter.shOverTime(item.getLeave_id() + "", "1");
                    }
                }
            }).show();
        }
    }

    @Override
    public void cancelApproveSuccess(BaseModel<Object> o) {
        rlChexiao.setVisibility(View.INVISIBLE);
        ToastUtils.showShort("已审批");
    }
}
